package com.gexin.core.interceptor;
import com.gexin.common.model.AjaxCmd;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.JFinal;
import com.jfinal.log.Log;
/**
 * APP拦截器
 * @author 肖得明
 *2015-12-29
 *
 */
public class AppInterceptor implements Interceptor {
	private static final Log log = Log.getLog(AppInterceptor.class);
	@Override
	public void intercept(Invocation paramInvocation)  {
		try {
//			Thread.sleep(6000);
			paramInvocation.invoke();
		}catch (Exception e) {
			doLog(paramInvocation,e);
			paramInvocation.getController().renderJson(new AjaxCmd(false, "服务器异常请稍后再试！"));
		}
	}
	
	private void doLog(Invocation ai,Exception e) {
        //开发模式  
        if(JFinal.me().getConstants().getDevMode()){  
            e.printStackTrace();  
        }  
        StringBuilder sb =new StringBuilder("\n---Exception Log Begin---\n");  
        sb.append("Controller:").append(ai.getController().getClass().getName()).append("\n");  
        sb.append("Method:").append(ai.getMethodName()).append("\n");  
        sb.append("Exception Type:").append(e.getClass().getName()).append("\n");  
        sb.append("Exception Details:");  
        log.error(sb.toString(), e);  
    }  
}